﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
namespace Ecreate.Data
{
    partial class SqliteDBEngine : DBEngine
    {
        private SQLiteConnection m_DbConnection;
        private SQLiteTransaction trans;
        public SqliteDBEngine(string connectionString)
        {
            this.trans = null;
            this.m_DbConnection = new SQLiteConnection(connectionString);
        }

        public override IDbTransaction BeginTransaction()
        {
            this.trans = this.m_DbConnection.BeginTransaction();
            return this.trans;
        }
        public override bool CommitTrans()
        {
            if (this.trans != null)
                this.trans.Commit();

            return true;
        }
        public override bool RollBack()
        {
            if (this.trans != null)
            {
                this.trans.Rollback();
            }
            return true;
        }

        public override DataBaseType DatabaseType
        {
            get { return DataBaseType.MSSQLServer; }
        }

        public override IDbConnection DbConnection
        {
            get
            {
                return this.m_DbConnection;
            }
        }


        private void PrepareCommand(SQLiteCommand cmd, CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            cmd.CommandType = commandType;
            cmd.CommandText = commandText;
            cmd.Connection = this.m_DbConnection;
            cmd.Transaction = this.trans;
            if ((commandParameters != null) && (commandParameters.Count > 0))
            {
                for (int i = 0; i < commandParameters.Count; i++)
                {
                    cmd.Parameters.AddWithValue(commandParameters[i].ParameterName, commandParameters[i].Value);
                    cmd.Parameters[i].Direction = commandParameters[i].Direction;
                }
            }
        }


        public override DataSet ExecuteDataSet(CommandType commandType, string commandText, QueryParameterCollection commandParameters, DataSet ds, string tableName)
        {
            base.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            this.PrepareCommand(cmd, commandType, commandText, commandParameters);
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            if (object.Equals(tableName, null) || (tableName.Length < 1))
            {
                da.Fill(ds);
            }
            else
            {
                da.Fill(ds, tableName);
            }
            this.GetQueryParameter(cmd, commandParameters);
            cmd.Parameters.Clear();
            base.Close();
            return ds;
        }

        public override DataSet ExecuteDataSet(SqlStruct sqlStruct)
        {
            base.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            DataSet ds = new DataSet();
            QueryParameterCollection commandParameters = new QueryParameterCollection();
            if (sqlStruct != null)
            {
                for (int i = 0; i < sqlStruct.ParamFields.Count; i++)
                {
                    commandParameters.Add(sqlStruct.ParamFields[i].ParamName, sqlStruct.ParamFields[i].ParamValue);
                }
                this.PrepareCommand(cmd, CommandType.Text, sqlStruct.SqlString, commandParameters);
            }
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            da.Fill(ds);
            cmd.Parameters.Clear();
            base.Close();
            return ds;
        }

        public override DataSet ExecuteDataSet(string commandText, QueryParameterCollection commandParameters, DataSet ds, int startRecord, int maxRecords, string tableName)
        {
            base.Open();
            SQLiteCommand cmd = new SQLiteCommand();
            this.PrepareCommand(cmd, CommandType.Text, commandText, commandParameters);
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            if (object.Equals(tableName, null) || (tableName.Trim() == string.Empty))
            {
                da.Fill(ds);
            }
            else
            {
                da.Fill(ds, startRecord, maxRecords, tableName);
            }
            cmd.Parameters.Clear();
            base.Close();
            return ds;
        }

        public override DataTable GetDataSchemaByTableName(string tableName)
        {
            try
            {
                base.Open();
                lock (this)
                {
                    string sql = "select * from " + tableName;
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, this.m_DbConnection);
                    DataSet ds = new DataSet();
                    adapter.FillSchema(ds, SchemaType.Mapped);
                    ds.Tables[0].TableName = tableName;
                    return ds.Tables[0];
                }
            }
            finally
            {
                base.Close();
            }
        }

        public override DataTable GetDataTable(string commandText)
        {
            try
            {
                base.Open();
                lock (this)
                {
                    string sql = commandText;
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, this.m_DbConnection);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
            finally
            {
                base.Close();
            }
        }
        public override string GetConnectionString()
        {
            return this.m_DbConnection.ConnectionString;
        }





    }
}
